﻿
Imports System.Net
Imports System.Threading
Imports std

Public Class httpServer4
    Inherits httpServer3

    Public Shadows Event receive(ByVal ctx As httpContext, ByRef ec As eventComb)
    Public Shadows Event reject(ByVal ctx As httpContext, ByRef ec As eventComb)

    Private Overloads Sub startContext(ByVal ctx As httpContext,
                                       ByVal raise As void(Of httpContext, eventComb),
                                       ByVal timeCounter As Int64)
        assert(Not raise Is Nothing)
        Dim startticks As Int64 = 0
        Dim inner As eventComb = Nothing
        assert_begin(New eventComb(Function() As Boolean
                                       startticks = Now.ticks()
                                       inner = eventComb.return(Sub(ByRef ec As eventComb)
                                                                    raise(ctx, ec)
                                                                End Sub)
                                       Return waitfor(inner) AndAlso goto_next()
                                   End Function,
                                   Function() As Boolean
                                       ctx.close()
                                       finishContext(ctx, startticks, timeCounter)
                                       Return inner.end_result() AndAlso goto_next()
                                   End Function))
    End Sub

    Protected Overrides Sub startContext(ByVal ctx As httpContext)
        startContext(ctx,
                     Sub(ByRef c, ByRef ec) RaiseEvent receive(c, ec),
                     HTTPSERVER3_REQUEST_RESPOND_TIMEMS)
    End Sub

    Protected Overrides Sub rejectContext(ByVal ctx As httpContext,
                                          ByVal rejectIssues As Byte)
        startContext(ctx,
                     Sub(ByRef c, ByRef ec)
                         c.setRejectIssues(rejectIssues)
                         RaiseEvent reject(c, ec)
                     End Sub,
                     HTTPSERVER3_REJECT_RESPOND_TIMEMS)
    End Sub
End Class
